package com.msmall.community.common.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.common.dao.CityDao;
import com.msmall.community.common.model.City;

public class CityDaoHibernate extends BaseDaoHibernate<City, String> implements CityDao{

	public CityDaoHibernate(){
		super(City.class);
	}
	/**
	 * 获得所有最高级别的区域信息，直辖市和省
	 * @return
	 */
	public List<City> getTopCities() throws Exception{
		String queryString = "from City where cityCode like '" + "%0000'";
		List list = super.getHibernateTemplate().find(queryString);
		return list;
	}
	/**
	 * 通过区划码获得直接子级的所有城市
	 * 需要判断当前区域所在级别， 1级获得2级， 2级获得3级， 如果是直辖市直接由1级获得3级
	 * @param code 110000
	 * @param direct 是否直辖市
	 * @param level 等级
	 * @return
	 * @throws Exception
	 */
	public List<City> getChildCities(String code, boolean direct, int level) throws Exception{
		List list = null;
		String queryString = "from City where cityCode <> '"+ code +"' and cityCode like " ;
		if(level == 3)
			return null;
		if(level == 1){
			if(direct){ //直辖市
				queryString = queryString + "'" + code.substring(0,2) + "%'";
			}else{
				queryString = queryString + "'" + code.substring(0,2) + "%" + "00'";
			}
		}
		if(level == 2){
			queryString = queryString + "'" + code.substring(0,4) + "%'";
		}
		list = getHibernateTemplate().find(queryString);
		return list;
	}
	/**
	 * 通过区划码获得直接子级的所有城市
	 * 需要判断当前区域所在级别， 1级获得2级， 2级获得3级， 如果是直辖市直接由1级获得3级
	 * 作为天气使用，如果数据库没有weather_code 值，则过滤掉
	 * @param code 110000
	 * @return
	 * @throws Exception
	 */
	public List<City> getWeatherChildCities(String code, boolean direct, int level) throws Exception{
		List list = null;
		String queryString = "from City where weatherCode <> null and weatherCode <> '' and cityCode <> '"+ code +"' and cityCode like " ;
		if(level == 3)
			return null;
		if(level == 1){
			if(direct){ //直辖市
				queryString = queryString + "'" + code.substring(0,2) + "%'";
			}else{
				queryString = queryString + "'" + code.substring(0,2) + "%" + "00'";
			}
		}
		if(level == 2){
			queryString = queryString + "'" + code.substring(0,4) + "%'";
		}
		list = getHibernateTemplate().find(queryString);
		return list;
	}
}
